package tech.aistar.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import tech.aistar.dto.Result;
import tech.aistar.entity.Collect;
import tech.aistar.entity.User;
import tech.aistar.service.ICompletionService;
import tech.aistar.service.IManchoiceService;
import tech.aistar.service.IProgrammingService;
import tech.aistar.service.ISinchoiceService;
import tech.aistar.service.impl.CollectServiceImpl;
import tech.aistar.service.impl.UserServiceImpl;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;



@Controller

@RequestMapping("/user")
public class UserAction {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    public ISinchoiceService sinchoiceService;

    @Autowired
    public IManchoiceService manchoiceService;

    @Autowired
    public ICompletionService completionService;
    @Autowired
    public CollectServiceImpl collectService;

    @Autowired
    public IProgrammingService programmingService;

    @RequestMapping("/info")
    public String userInfo(HttpSession session,Model model) {
        User user = (User) session.getAttribute("user");

        model.addAttribute("mycollection", userService.findcol(1,10,user.getUsId()));
        model.addAttribute("level", userService.getUserLevel(user.getUsId()));
        model.addAttribute("tasks", userService.getUserTask(String.valueOf(user.getUsId())));
        model.addAttribute("wrongList", userService.getUserWrongtopics(Long.valueOf(user.getUsId())));
        model.addAttribute("score", userService.getUserExa(String.valueOf(user.getUsId())));
        return "person";
    }

    @RequestMapping("/person")
    public @ResponseBody
    Result personInfo(HttpServletRequest request, HttpSession session) {
        String uname = request.getParameter("uname");
        User user = userService.findUserBynickname(uname);
        session.setAttribute("user", user);
        return new Result("700", "123");
    }

    @RequestMapping("/update")
    public @ResponseBody
    Result updateUser(User user) {
        int count = userService.updateUser(user);
        if (count > 0) {
            return new Result("12", "更新成功");
        } else {
            return new Result("12", "更新失败");
        }
    }

    @RequestMapping("/upload")
    public @ResponseBody
    String upload(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpSession session){
        String nickname = req.getParameter("nickname");
        try {
            String path = userService.saveImage(file);
            int count = userService.uploadimg(path,nickname);
            User user = userService.findUserBynickname(nickname);
            session.setAttribute("user",user);
            System.out.println(path);
            return "{\"code\":\"0\",\"msg\":\"1\",\"data\":\""+path+"\"}";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/change")
    @ResponseBody
    public PageInfo<Collect> changepage(HttpSession session,HttpServletRequest request){
        String collectpage = request.getParameter("page");
        User user = (User) session.getAttribute("user");
        return userService.findcol(Integer.parseInt(collectpage),10,user.getUsId());
    }
}



